/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProductLevel.java
 *
 * Created on Nov 16, 2011, 10:04:41 PM
 */
package VIMS;

import java.awt.*;
import java.sql.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Alex
 */
public class ProductLevel extends javax.swing.JFrame {

    static private DefaultTableModel Tmodel = new DefaultTableModel();
    static private ResultSetMetaData md = null;
    static private ResultSet rset=null;
    
    static String host="jdbc:mysql://localhost:3306/vims";
    static String username="root";
    static String password="123";
    static private String UserName=null;
    static private Database dbOB  = new Database();
    
    /** Creates new form ProductLevel */
    public ProductLevel() {
        super("Change Product Level");
        initComponents();
        setLocationRelativeTo(null);
        
        String SQL = "SELECT P_NUMBER,P_NAME,P_VOLUME,P_PACKAGE,P_PRICE,PL_QUANTITY FROM PRODUCT ";
        rset = dbOB.getResultSet(SQL);
        jTable1.setModel(PopulateTable(rset));  
    }
    
    private DefaultTableModel PopulateTable(ResultSet rset)
 {
        try
        {
//          ResultSetMetaData md = (ResultSetMetaData) rset.getMetaData();
            md = rset.getMetaData();                //md,rset,Tmodel -> Global
            int columnCount = md.getColumnCount();
            Vector columns = new Vector(columnCount+1);

            //store column names
            for(int i=1; i<=columnCount; i++)
                columns.add(md.getColumnName(i));
            columns.add("Replenish Product Level");
            
            Vector data = new Vector();
            Vector row;

            //store row data
            while(rset.next())
            { 
                row = new Vector(columnCount+1);
                for(int i=1; i<=columnCount+1; i++){
                    if(i==columnCount+1)
                        row.add("");
                    else
                        row.add(rset.getString(i));
                }
                data.add(row);
            }

            Tmodel.setDataVector(data, columns);
            return Tmodel;
        }
        catch (Exception e)
        {
            System.out.println("SQL Exception: "+ e.toString());
        }
        return null;
 }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ProductLevelScrollPane = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        ProductLevelPagePanel = new javax.swing.JPanel();
        PageLabel4 = new javax.swing.JLabel();
        CurrentPageLabel4 = new javax.swing.JLabel();
        TotalPageLabel4 = new javax.swing.JLabel();
        Slesh4 = new javax.swing.JLabel();
        Separator11 = new javax.swing.JLabel();
        Separator12 = new javax.swing.JLabel();
        JumptoTextField4 = new javax.swing.JTextField();
        JumptoLabel4 = new javax.swing.JLabel();
        JumpButton4 = new javax.swing.JButton();
        TotalRecordLabel4 = new javax.swing.JLabel();
        RecordFoundLabel4 = new javax.swing.JLabel();
        Separator13 = new javax.swing.JLabel();
        PreviousButton4 = new javax.swing.JButton();
        NextButton4 = new javax.swing.JButton();
        FuctionPanel2 = new javax.swing.JPanel();
        ViewButton4 = new javax.swing.JButton();
        CancelButton5 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(800, 600));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Number", "Name", "Volume", "Package", "Price", "Product Level", "New Product Level"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.getTableHeader().setReorderingAllowed(false);
        ProductLevelScrollPane.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        jTable1.getColumnModel().getColumn(2).setResizable(false);
        jTable1.getColumnModel().getColumn(3).setResizable(false);
        jTable1.getColumnModel().getColumn(4).setResizable(false);
        jTable1.getColumnModel().getColumn(5).setResizable(false);
        jTable1.getColumnModel().getColumn(6).setResizable(false);

        ProductLevelPagePanel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        PageLabel4.setFont(new java.awt.Font("Times New Roman", 1, 12));
        PageLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        PageLabel4.setText("Page ");

        CurrentPageLabel4.setFont(new java.awt.Font("Times New Roman", 1, 12));
        CurrentPageLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        CurrentPageLabel4.setText("?");

        TotalPageLabel4.setFont(new java.awt.Font("Times New Roman", 1, 12));
        TotalPageLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        TotalPageLabel4.setText("?");

        Slesh4.setFont(new java.awt.Font("Times New Roman", 1, 12));
        Slesh4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        Slesh4.setText("/");

        Separator11.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        Separator12.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        JumptoLabel4.setFont(new java.awt.Font("Times New Roman", 1, 12));
        JumptoLabel4.setText("Jump to");

        JumpButton4.setText("Jump");

        TotalRecordLabel4.setFont(new java.awt.Font("Times New Roman", 1, 12));
        TotalRecordLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        TotalRecordLabel4.setText("?");

        RecordFoundLabel4.setFont(new java.awt.Font("Times New Roman", 1, 12));
        RecordFoundLabel4.setText("Records Found");

        Separator13.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        PreviousButton4.setText("Previous");

        NextButton4.setText("Next");

        javax.swing.GroupLayout ProductLevelPagePanelLayout = new javax.swing.GroupLayout(ProductLevelPagePanel);
        ProductLevelPagePanel.setLayout(ProductLevelPagePanelLayout);
        ProductLevelPagePanelLayout.setHorizontalGroup(
            ProductLevelPagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ProductLevelPagePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(Separator11, javax.swing.GroupLayout.PREFERRED_SIZE, 4, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(PreviousButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(PageLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(CurrentPageLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Slesh4, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TotalPageLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(NextButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(16, 16, 16)
                .addComponent(Separator12, javax.swing.GroupLayout.PREFERRED_SIZE, 4, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(TotalRecordLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(14, 14, 14)
                .addComponent(RecordFoundLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Separator13, javax.swing.GroupLayout.PREFERRED_SIZE, 6, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addComponent(JumptoLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(JumptoTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(JumpButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(35, Short.MAX_VALUE))
        );
        ProductLevelPagePanelLayout.setVerticalGroup(
            ProductLevelPagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ProductLevelPagePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ProductLevelPagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(ProductLevelPagePanelLayout.createSequentialGroup()
                        .addComponent(NextButton4)
                        .addGap(12, 12, 12))
                    .addGroup(ProductLevelPagePanelLayout.createSequentialGroup()
                        .addComponent(Separator11, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11))
                    .addGroup(ProductLevelPagePanelLayout.createSequentialGroup()
                        .addComponent(PreviousButton4)
                        .addGap(12, 12, 12))
                    .addGroup(ProductLevelPagePanelLayout.createSequentialGroup()
                        .addComponent(PageLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                        .addGap(11, 11, 11))
                    .addGroup(ProductLevelPagePanelLayout.createSequentialGroup()
                        .addComponent(CurrentPageLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                        .addGap(11, 11, 11))
                    .addGroup(ProductLevelPagePanelLayout.createSequentialGroup()
                        .addComponent(Slesh4, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11))
                    .addGroup(ProductLevelPagePanelLayout.createSequentialGroup()
                        .addComponent(TotalPageLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                        .addGap(11, 11, 11))
                    .addGroup(ProductLevelPagePanelLayout.createSequentialGroup()
                        .addComponent(Separator12, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                        .addGap(11, 11, 11))
                    .addGroup(ProductLevelPagePanelLayout.createSequentialGroup()
                        .addComponent(TotalRecordLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11))
                    .addGroup(ProductLevelPagePanelLayout.createSequentialGroup()
                        .addComponent(RecordFoundLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11))
                    .addGroup(ProductLevelPagePanelLayout.createSequentialGroup()
                        .addComponent(Separator13, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11))
                    .addGroup(ProductLevelPagePanelLayout.createSequentialGroup()
                        .addComponent(JumptoLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11))
                    .addGroup(ProductLevelPagePanelLayout.createSequentialGroup()
                        .addComponent(JumptoTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                        .addGap(11, 11, 11))
                    .addGroup(ProductLevelPagePanelLayout.createSequentialGroup()
                        .addComponent(JumpButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12))))
        );

        FuctionPanel2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        ViewButton4.setText("Save");
        ViewButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ViewButton4ActionPerformed(evt);
            }
        });

        CancelButton5.setText("Cancel");
        CancelButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout FuctionPanel2Layout = new javax.swing.GroupLayout(FuctionPanel2);
        FuctionPanel2.setLayout(FuctionPanel2Layout);
        FuctionPanel2Layout.setHorizontalGroup(
            FuctionPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, FuctionPanel2Layout.createSequentialGroup()
                .addContainerGap(591, Short.MAX_VALUE)
                .addComponent(ViewButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(CancelButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        FuctionPanel2Layout.setVerticalGroup(
            FuctionPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(FuctionPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(FuctionPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CancelButton5)
                    .addComponent(ViewButton4))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ProductLevelScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
                    .addComponent(FuctionPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(ProductLevelPagePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ProductLevelScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ProductLevelPagePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 195, Short.MAX_VALUE)
                .addComponent(FuctionPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ViewButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ViewButton4ActionPerformed
        // TODO add your handling code here:
            Object number=0,product1=0,product2=0;
            int truecount=0;
            boolean change =false;
            
            for(int i=0;i<jTable1.getRowCount();i++)
            if(jTable1.getValueAt(i, 6) != "")
            change=true;

            String message = "Update Product Level?";
            String title = "Confirmation?";
            if(change==true)
            {
            int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (reply == JOptionPane.YES_OPTION)
            {
                for(int i=0;i<jTable1.getRowCount();i++)
                {
                    number=jTable1.getValueAt(i, 0);
                    product1=jTable1.getValueAt(i, 5);
                    product2=jTable1.getValueAt(i, 6);
                    
                    if (product2 == "")
                        product2=0;
                    System.out.println(number+" "+product1+" "+product2);

                            String SQL="UPDATE PRODUCT SET PL_QUANTITY = "+(Integer.parseInt(product1.toString())+Integer.parseInt(product2.toString()))+" WHERE  P_NUMBER = "+number;
                            System.out.println(SQL);
                            if(dbOB.update(SQL)==true)
                                truecount++;
                            else
                                JOptionPane.showMessageDialog(null, "Data failed to be added into database.\nPlease check the entered information!");
                 }    
             }
            if(truecount==jTable1.getRowCount())
            {
                JOptionPane.showMessageDialog(null,"Product Level Updated.");
                this.setVisible(false);
            }
            }
            else
                JOptionPane.showMessageDialog(null,"Nothing to be Update!");
    }//GEN-LAST:event_ViewButton4ActionPerformed

    private void CancelButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButton5ActionPerformed

        this.setVisible(false);
    }//GEN-LAST:event_CancelButton5ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ProductLevel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ProductLevel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ProductLevel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ProductLevel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ProductLevel().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton5;
    private javax.swing.JLabel CurrentPageLabel4;
    private javax.swing.JPanel FuctionPanel2;
    private javax.swing.JButton JumpButton4;
    private javax.swing.JLabel JumptoLabel4;
    private javax.swing.JTextField JumptoTextField4;
    private javax.swing.JButton NextButton4;
    private javax.swing.JLabel PageLabel4;
    private javax.swing.JButton PreviousButton4;
    private javax.swing.JPanel ProductLevelPagePanel;
    private javax.swing.JScrollPane ProductLevelScrollPane;
    private javax.swing.JLabel RecordFoundLabel4;
    private javax.swing.JLabel Separator11;
    private javax.swing.JLabel Separator12;
    private javax.swing.JLabel Separator13;
    private javax.swing.JLabel Slesh4;
    private javax.swing.JLabel TotalPageLabel4;
    private javax.swing.JLabel TotalRecordLabel4;
    private javax.swing.JButton ViewButton4;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
